package com.gulimall.order.config.mq;

import com.common.to.mq.SeckillOrderTo;
import com.gulimall.order.service.impl.OrderServiceImpl;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author Rookie-6688
 * @Description
 * @Create 2021-05-17 11:33
 */
@Slf4j
@Component
@RabbitListener(queues = "order.seckill.order.queue")
public class SeckillListener {

    @Resource
    private OrderServiceImpl orderServiceImpl;

    @RabbitHandler
    public void handlerSeckillQueue(SeckillOrderTo seckillOrderTo, Message message, Channel channel) throws IOException {
        try {
            log.info("进入秒杀的消息队列消费者----------------------");
            orderServiceImpl.handlerSeckillQueue(seckillOrderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
